// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2016-2019, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * lombo fwupgrade driver - Driver of LomboTech
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/proc_fs.h>
#include <mach/csp.h>
#include <mach/lombo_pmc.h>

#define NET_FW_UPGRADE_NAME	"lombo-fwupgrade"
#define PMC_NET_FW_UPDATE	0xecd00002U
#define PMC_SET_FW_UPDATE	0xecd00001U

#ifndef BIT
	#define BIT(x)	(1 << (x))
#endif

static long fwupgrade_ioctl(struct file *filp, unsigned int cmd,
			unsigned long args)
{
	switch (cmd) {
	case PMC_NET_FW_UPDATE:
		lombo_set_net_fw_update((u32)args);
		break;
	case PMC_SET_FW_UPDATE:
		lombo_set_ota_fw_update((u32)args);
		break;
	default:
		pr_err("Not support this ioctl now!\n");
		return -EINVAL;
	}
	return 0;
}

static const struct file_operations fwupgrade_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = fwupgrade_ioctl,
};

static struct miscdevice lombo_update_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = NET_FW_UPGRADE_NAME,
	.fops = &fwupgrade_fops
};

static int __init fwupgrade_init(void)
{
	int ret;

	ret = misc_register(&lombo_update_dev);
	if (ret != 0) {
		pr_err("%s register failed!\n", NET_FW_UPGRADE_NAME);
		ret = __LINE__;
	}
	return ret;
}

static void __exit fwupgrade_exit(void)
{
	misc_deregister(&lombo_update_dev);
}

module_init(fwupgrade_init);
module_exit(fwupgrade_exit);

MODULE_AUTHOR("lomboswer@lombotech.com");
MODULE_DESCRIPTION("Driver for LomboTech fwupgrade module");
MODULE_LICENSE("GPL");
